#include "sprite.h"
#include "global.h"


CSprite::CSprite(CTexture* texture, int start, int end, int timeAnimation)
	: _texture(texture)
{
	_start = start;
	_end = end;
	_timeAni = timeAnimation;
	_index = start;
	_timeLocal = 0;
}

CSprite::CSprite(CTexture* texture, int timeAnimation)
	: _texture(texture)
{
	_start = 0;
	_end = _texture->Count - 1;
	_timeAni = timeAnimation;
	_index = 0;
	_timeLocal = 0;
}

CSprite::~CSprite()
{
	delete _texture;
}

void CSprite::Next() 
{
	_index++;
	if(_index > _end)
		_index = _start;
}

void CSprite::Next(int beginIndex, int endIndex) 
{
	//_Index = (_Index + _Count -1) % _Count;
	if(beginIndex < endIndex) 
	{
		_index++;
		if(_index < beginIndex || _index > endIndex) _index = beginIndex;
	}
	else 
	{
		_index--;
		if(_index > beginIndex || _index < endIndex) _index = beginIndex;
	}
		
	/*else
		_Index = (_Index + (endIndex - beginIndex + 1) + 1) % (endIndex - beginIndex + 1) + beginIndex;*/
}

void CSprite::Reset()
{
	_index = _start;
	_timeLocal = 0;
}

void CSprite::SelectIndex(int index) 
{
	_index = index;
}

void CSprite::Update(int ellapseTime)
{
	_timeLocal += ellapseTime;

	if(_timeLocal >= _timeAni) {
		_timeLocal = 0;
		//
		this->Next();
	}
}

void CSprite::Draw(int X, int Y)
{
	RECT srect;

	srect.left = (_index % _texture->Cols)*(_texture->FrameWidth);// + 1;
	srect.top = (_index / _texture->Cols)*(_texture->FrameHeight);// + 1;
	srect.right = srect.left + _texture->FrameWidth;
	srect.bottom = srect.top + _texture->FrameHeight;// + 1;

	D3DXVECTOR3 position((float)X, (float)Y, 0);
	
	G_SpriteHandler->Draw(
		_texture->Texture,
		&srect,
		NULL,
		&position,
		0xFFFFFFFF //color
	);
	
}

void CSprite::DrawFlipX(int x, int y)
{
	D3DXMATRIX oldMt;
	G_SpriteHandler->GetTransform(&oldMt);
	
	D3DXMATRIX newMt;
	D3DXVECTOR2 center = D3DXVECTOR2(x + _texture->FrameWidth / 2, y + _texture->FrameHeight / 2);
	D3DXVECTOR2 rotate = D3DXVECTOR2(-1, 1);

	D3DXMatrixTransformation2D(&newMt, &center, 0.0f, &rotate, NULL, 0.0f, NULL);
	D3DXMATRIX finalMt = newMt * oldMt ;
	G_SpriteHandler->SetTransform(&finalMt);

	this->Draw(x, y);

	G_SpriteHandler->SetTransform(&oldMt);
}

// tieu nun
void CSprite::DrawFlipY(int x, int y)
{
	D3DXMATRIX oldMt;
	G_SpriteHandler->GetTransform(&oldMt);

	D3DXMATRIX newMt;
	D3DXVECTOR2 center = D3DXVECTOR2(x + _texture->FrameWidth / 2, y + _texture->FrameHeight / 2);
	D3DXVECTOR2 rotate = D3DXVECTOR2(1, -1);

	D3DXMatrixTransformation2D(&newMt, &center, 0.0f, &rotate, NULL, 0.0f, NULL);
	D3DXMATRIX finalMt = newMt * oldMt ;
	G_SpriteHandler->SetTransform(&finalMt);

	this->Draw(x, y);

	G_SpriteHandler->SetTransform(&oldMt);
}

// tieu nun
void CSprite::DrawRect(int X, int Y, RECT SrcRect)
{
	D3DXVECTOR3 position((float)X, (float)Y, 0);
	G_SpriteHandler->Draw(
		_texture->Texture,
		&SrcRect,
		NULL,
		&position,
		D3DCOLOR_XRGB(255, 255, 255)
		);
}

void CSprite::DrawTransform(int x, int y, D3DXVECTOR2 scale, float degRotate, float depth)
{
	RECT srect;

	srect.left = (_index % _texture->Cols)*(_texture->FrameWidth) + 1;
	srect.top = (_index / _texture->Cols)*(_texture->FrameHeight) + 1;
	srect.right = srect.left + _texture->FrameWidth ;
	srect.bottom = srect.top + _texture->FrameHeight + 1;

	//save old matrix
	D3DXMATRIX oldMt;
	G_SpriteHandler->GetTransform(&oldMt);

	//create new matrix
	D3DXMATRIX newMt;
	D3DXVECTOR2 center = D3DXVECTOR2(x + _texture->FrameWidth / 2, y + _texture->FrameHeight / 2);

	D3DXMatrixTransformation2D(
		&newMt,
		&center,			//scale center
		0.0f,				//scaling rotate
		&scale,				//scale
		&center,			//rotate center
		D3DXToRadian(degRotate),	//rotate degRotate ^o
		NULL);

	//multi
	D3DXMATRIX finalMt = newMt * oldMt ;
	G_SpriteHandler->SetTransform(&finalMt);

	G_SpriteHandler->Draw(
		_texture->Texture,
		&srect,
		NULL,
		&D3DXVECTOR3(x, y, depth),
		0xFFFFFFFF
		);

	G_SpriteHandler->SetTransform(&oldMt);
}